package leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * Leetcode456【132 模式】
 * https://leetcode-cn.com/problems/132-pattern/
 */
public class OneTwoThreeModel {//看清题35034也是只要满足ijk条件即可不考虑顺序

    public boolean find132pattern(int[] nums) {

        Stack<Integer> stack = new Stack<>();//存放nums[k]
        //这里为啥要用Integer.MIN_VALUE因为需要比较当前元素是不是比medianJudgment小先比较会存在第二个直接跳出问题
        int medianJudgment = Integer.MIN_VALUE;//实际比较的nums[k]
        if (nums.length < 3){
            return false;
        }
        for (int i = nums.length - 1; i >= 0  ; i--) {
            if(nums[i] < medianJudgment){
                return true;
            }
            while (!stack.isEmpty() && stack.peek() < nums[i]){
                medianJudgment = stack.pop();
            }
            stack.push(nums[i]);
        }
        return false;
    }
}
